package com.gaofans.vertx.boot.dbutils.config;

import com.gaofans.vertx.boot.common.config.VertxAutoConfig;
import com.gaofans.vertx.boot.dbutils.utils.DbConstant;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.PoolOptions;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(VertxAutoConfig.class)
@ConditionalOnClass(PgPool.class)
public class PostgresqlAutoConfig {

    @Bean
    @ConfigurationProperties(prefix = DbConstant.PG_OPTIONS)
    public PgConnectOptions pgOptions(){
        return new PgConnectOptions();
    }

    @Bean
    @ConfigurationProperties(prefix = DbConstant.POOL_OPTIONS)
    public PoolOptions poolOptions(){
        return new PoolOptions();
    }

    @Bean
    public PgPool pgPool(Vertx vertx,
                         PgConnectOptions pgOptions,
                         PoolOptions poolOptions){
        return PgPool.pool(vertx,pgOptions,poolOptions);
    }
}
